package com.gs.m_home.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.gs.common.API
import com.gs.common.data.CartData
import com.gs.common.global.GlobalVariables
import com.gs.common.util.ToastUtil
import com.gs.m_home.R

class CartAdapter(context: Context, data: ArrayList<CartData>): RecyclerView.Adapter<CartAdapter.CartViewHolder>() {

    private val data: ArrayList<CartData>
    private val context: Context

    private var mOnItemClickListener: OnCartItemClickListener? = null
    private var mOnCartItemAddCountBtnClickListener: OnCartItemAddCountBtnClickListener? = null
    private var mOnCartItemSubtractCountBtnClickListener: OnCartItemSubtractCountBtnClickListener? = null
    private var mOnCartItemDeleteBtnClickListener: OnCartItemDeleteBtnClickListener? = null
    private var mOnCartItemLeftLayoutClickListener: OnCartItemLeftLayoutClickListener? = null

    inner class CartViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val picture: ImageView
        val goodTitle: TextView
        val goodDetailTitle: TextView
        val tel: TextView
        val goodPrice: TextView
        val soldCount: TextView
        private val operationLayout: RelativeLayout
        private val addCountBtn: ImageView
        private val subtractCountBtn: ImageView
        private val deleteButtonLayout: RelativeLayout
        private val deleteButton: TextView
        private val leftLayout: RelativeLayout

        init {
            picture = itemView.findViewById(R.id.cart_good_img)
            goodTitle = itemView.findViewById(R.id.cart_good_title_tv)
            goodDetailTitle = itemView.findViewById(R.id.cart_good_detail_title_tv)
            tel = itemView.findViewById(R.id.cart_good_tel_tv)
            goodPrice = itemView.findViewById(R.id.cart_good_price)
            soldCount = itemView.findViewById(R.id.cart_good_sold_count_tv)
            operationLayout = itemView.findViewById(R.id.operation_layout)
            addCountBtn = itemView.findViewById(R.id.add_count_btn)
            subtractCountBtn = itemView.findViewById(R.id.subtract_count_btn)
            deleteButtonLayout = itemView.findViewById(R.id.delete_button_layout)
            deleteButton = itemView.findViewById(R.id.delete_button_tv)
            leftLayout = itemView.findViewById(R.id.cart_item_left_layout)

            addCountBtn.setOnClickListener {
                if (data.size == 0 || data[adapterPosition].goodSoldCount >= 99) return@setOnClickListener
                mOnCartItemAddCountBtnClickListener?.onClick(adapterPosition, data[adapterPosition].orderId ,data[adapterPosition].good.id)
            }

            subtractCountBtn.setOnClickListener {
                if (data.size == 0 || data[adapterPosition].goodSoldCount <=1  ) return@setOnClickListener
                mOnCartItemSubtractCountBtnClickListener?.onClick(adapterPosition, data[adapterPosition].orderId, data[adapterPosition].good.id)
            }

            deleteButton.setOnClickListener {
                if (data.size == 0) return@setOnClickListener
                mOnCartItemDeleteBtnClickListener?.onClick(adapterPosition, data[adapterPosition].orderId, data[adapterPosition].good.id)
            }

            leftLayout.setOnClickListener {
                mOnCartItemLeftLayoutClickListener?.onClick(adapterPosition, data[adapterPosition].orderId, data[adapterPosition].good.id)
            }

            GlobalVariables.deleteSingle.addObserver(object : com.gs.common.observer.ValueObserver<Boolean>{
                override fun update(value: Boolean) {
                    if (value) {
                        operationLayout.visibility = View.GONE
                        deleteButtonLayout.visibility = View.VISIBLE
                    } else {
                        operationLayout.visibility = View.VISIBLE
                        deleteButtonLayout.visibility = View.GONE
                    }
                }
            })

        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CartViewHolder {
        val view = View.inflate(context, R.layout.cart_item, null)
        return CartViewHolder(view)
    }

    override fun getItemCount(): Int {
        return data.size
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: CartViewHolder, position: Int) {
        Glide.with(context).load("http://${API.IP}:${API.PORT}${data[position].good.picture_path}").into(holder.picture)
        holder.goodTitle.text = "标题：" + data[position].good.title
        holder.goodDetailTitle.text = "详细标题：" + data[position].good.detail_title
        holder.tel.text = "商家电话：" + data[position].businessTel
        holder.goodPrice.text = data[position].goodTotalMoney.toString()
        holder.soldCount.text = data[position].goodSoldCount.toString()
    }

    interface OnCartItemLeftLayoutClickListener {
        fun onClick(position: Int, orderId: Int, goodId: Int)
    }

    interface OnCartItemClickListener {
        fun onItemClick(position: Int, orderId: Int, goodId: Int)
    }

    interface OnCartItemAddCountBtnClickListener {
        fun onClick(position: Int, orderId: Int, goodId: Int)
    }

    interface OnCartItemSubtractCountBtnClickListener {
        fun onClick(position: Int, orderId: Int, goodId: Int)
    }

    interface OnCartItemDeleteBtnClickListener {
        fun onClick(position: Int, orderId: Int, goodId: Int)
    }

    fun setCartItemLeftLayoutClickListener(listener: OnCartItemLeftLayoutClickListener) {
        this.mOnCartItemLeftLayoutClickListener = listener
    }

    fun setCartItemClickListener(listener: OnCartItemClickListener) {
        mOnItemClickListener = listener
    }

    fun setCartItemAddCountBtnClickListener(listener: OnCartItemAddCountBtnClickListener) {
        mOnCartItemAddCountBtnClickListener = listener
    }

    fun setCartItemSubtractCountBtnClickListener(listener: OnCartItemSubtractCountBtnClickListener) {
        mOnCartItemSubtractCountBtnClickListener = listener
    }

    fun setCartItemDeleteBtnClickListener(listener: OnCartItemDeleteBtnClickListener) {
        mOnCartItemDeleteBtnClickListener = listener
    }

    fun removeCartItemAddCountBtnClickListener() {
        if (mOnCartItemAddCountBtnClickListener != null) {
            mOnCartItemAddCountBtnClickListener = null
        }
    }

    fun removeCartItemSubtractCountBtnClickListener() {
        if (mOnCartItemSubtractCountBtnClickListener != null) {
            mOnCartItemSubtractCountBtnClickListener = null
        }
    }

    fun removeCartItemDeleteBtnClickListener() {
        if (mOnCartItemDeleteBtnClickListener != null) {
            mOnCartItemDeleteBtnClickListener = null
        }
    }

    fun removeCartItemClickListener() {
        if (mOnItemClickListener != null) {
            mOnItemClickListener = null
        }
    }

    init {
        this.data = data
        this.context = context
    }

}